local houses_prefab_name = {
    "pighouse",
    "rabbithouse",
}

for k, prefab_name in pairs(houses_prefab_name) do
    AddPrefabPostInit(
    prefab_name,
    function(inst)
    -----------------------------------------------------------------------------------
        if not TheWorld.ismastersim then
            return
        end

        inst:AddComponent("npc_base_lib")
        inst:AddComponent("npc_everything_data")




        ------------------------------ 标记该建筑为玩家建造的
        inst:ListenForEvent("onbuilt",function(inst,_table)
            if _table and _table.builder and _table.builder:HasTag("player") then
                inst.npc_everything_data:Set("onbuilt_by_player",true)
            end
        end)
        inst:DoTaskInTime(0,function()
            if inst.npc_everything_data:Get("onbuilt_by_player") then
                inst:AddTag("onbuilt_by_player")
            end
        end)
        ----------------------------------------------------
        ----- OnLoad 的时候检查是否出售
        inst:ListenForEvent("tag_for_sale_task",function()
            if inst.npc_everything_data:Get("for_sale") == true and inst.npc_everything_data:Get("onbuilt_by_player") == true then
                inst.npc_base_lib:For_Sale_Sign_Show()

                if TUNING.NPC_CONFIG.REWARDS_LESS == true then 
                    ---- 如果是低奖励模式，则关闭猪人/兔人刷新任务
                    if inst.inittask then   ------ 停掉任务
                        inst.inittask:Cancel()
                    end
                    if inst.components.spawner then     ---- 停掉原住民刷新
                        -- inst:RemoveComponent("spawner")
                        pcall(function()
                            inst.components.spawner.externaltimerfinished = false
                            inst.components.worldsettingstimer:StopTimer("Spawner_SpawnDelay")
                        end)
                    end                
                    if inst.components.playerprox then  ---- 停掉检测玩家                    
                        -- inst:RemoveComponent("playerprox")
                        local onnear_fn = inst.components.playerprox.onnear
                        local onfar_fn = inst.components.playerprox.onfar
                        inst.components.playerprox:SetOnPlayerNear(onfar_fn)
                        inst.components.playerprox:SetOnPlayerFar(onnear_fn)
                    end

                end
                           
            end
        end)
        inst:DoTaskInTime(0,function()
            inst:PushEvent("tag_for_sale_task")
        end)
        ----- 处理被烧毁的情况
        inst:ListenForEvent("onburnt",function()
            inst.npc_everything_data:Set("onbuilt_by_player",false)
            inst:RemoveTag("onbuilt_by_player")
            inst.npc_base_lib:For_Sale_Task_Cancel()
        end)
    -----------------------------------------------------------------------------------
    end
    )
end
